﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Admin.Controllers
{
    public class AgentController : BaseController
    {
        // GET: Admin/Agent
        public ActionResult Index(int? page, int? count, int? level)
        {
            string Userlevel = "1";
            Userlevel = GetCookie("level");
            ViewBag.level = Userlevel;
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            List<AngentModel> list = new List<AngentModel>();
            ViewBag.level = level?.ToString() ?? string.Empty;
            using (var db = new qianjingEntities())
            {
                var query = db.D_Angent.AsQueryable();
                if (!string.IsNullOrEmpty(level.ToString()))
                {
                    query = query.Where(a => a.datLevel == level);
                }
                totalCount = query.Count();
                var records = query.OrderByDescending(a => a.datLevel).ThenBy(a => a.DATID).Where(a => a.RowStatus == 1).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
                foreach (var item in records)
                {
                    string datDescH = "";
                    if (!string.IsNullOrEmpty(item.datDATID.ToString()))
                    {
                        datDescH = db.D_Angent.FirstOrDefault(a => a.DATID == item.datDATID).datDesc;
                    }
                    list.Add(new AngentModel
                    {

                        DATID = Convert.ToInt16(item.DATID),
                        datLevel = item.datLevel,
                        datDesc = item.datDesc,
                        datDATID = Convert.ToInt16(item.datDATID),
                        RowStatus = item.RowStatus,
                        datDescH = datDescH

                    });
                }

            }


            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;

            return View(list);
        }
        //HttpGet: Admin/Agent
        [HttpGet]
        public AgentControllerResponse remove(long id)
        {
            AgentControllerResponse resp = new AgentControllerResponse();

            using (var db = new qianjingEntities())
            {

                var query = db.D_Angent.FirstOrDefault(a => a.DATID == id);
                var csm = db.D_Angent.Where(a => a.datDATID == query.DATID).ToList();
                if (csm != null)
                {
                    foreach (var item in csm)
                    {
                        var csm2 = db.D_Angent.Where(a => a.datDATID == item.DATID).ToList();
                        item.RowStatus = 2;
                        db.SaveChanges();
                        if (csm2 != null)
                        {
                            foreach (var i in csm2)
                            {
                                i.RowStatus = 2;
                                db.SaveChanges();
                            }
                        }

                    }
                }

                if (query == null)
                {
                    resp.ErrMsg = "该记录不存在";
                    return resp;
                }
                query.RowStatus = 2;

                db.SaveChanges();
            }
            return resp;


        }
        // GET: Admin/Add
        public ActionResult Add()
        {
            List<AngentModel> list = new List<AngentModel>();
            using (var db = new qianjingEntities())
            {
                var query = db.D_Angent.OrderBy(a => a.DATID).Where(a => a.datLevel == 1 && a.RowStatus == 1).ToList();
                foreach (var item in query)
                {
                    list.Add(new AngentModel
                    {
                        DATID = Convert.ToInt16(item.DATID),
                        datDesc = item.datDesc,
                        datLevel = item.datLevel,
                        datDATID = Convert.ToInt16(item.datDATID),

                    });
                }
                var level2 = db.D_Angent.OrderBy(a => a.DATID).Where(a => a.datLevel == 2 && a.RowStatus == 1).ToList();
                ViewBag.level2 = level2;

            }
            return View(list);
        }

        // POST: Admin/Add
        [HttpPost]
        public ActionResult Add(string data)
        {
            AppSetting setting = new AppSetting();
            var post = JObject.Parse(data);

            string addressDesc = post["addressDesc"].Value<string>();
            int level = post["level"].Value<int>();
            int level1ID = post["level1ID"].Value<int>();
            int level2ID = post["level2ID"].Value<int>();

            using (var db = new qianjingEntities())
            {
                if (level == 1)
                {
                    db.D_Angent.Add(new D_Angent
                    {
                        datDesc = addressDesc,
                        RowStatus = 1,
                        CreatedDate = DateTime.Now,
                        datLevel = 1
                    });

                }
                if (level == 2)
                {
                    db.D_Angent.Add(new D_Angent
                    {
                        datDesc = addressDesc,
                        RowStatus = 1,
                        CreatedDate = DateTime.Now,
                        datLevel = 2,
                        datDATID = level1ID
                    });

                }
                if (level == 3)
                {
                    db.D_Angent.Add(new D_Angent
                    {
                        datDesc = addressDesc,
                        RowStatus = 1,
                        CreatedDate = DateTime.Now,
                        datLevel = 3,
                        datDATID = level2ID
                    });

                }
                db.SaveChanges();
            }

            return RedirectToAction("Add");

        }

        // GET: Admin/Edit
        [HttpPost]
        public ActionResult Edit(string data)
        {
            AppSetting setting = new AppSetting();
            var post = JObject.Parse(data);

            string AgentDesc = post["AgentDesc"].Value<string>();
            int id = post["id"].Value<int>();

            using (var db = new qianjingEntities())
            {
                var query = db.D_Angent.FirstOrDefault(a => a.DATID == id);
                query.datDesc = AgentDesc;
                query.UpdatedDate = DateTime.Now;
                db.SaveChanges();
            }

            return RedirectToAction("Index");

        }

        // GET: Admin/Edit
        public ActionResult Edit(int id)
        {
            AngentModel model = new AngentModel();
            using (var db = new qianjingEntities())
            {
                var query = db.D_Angent.FirstOrDefault(a => a.DATID == id);
                if (query != null)
                {
                    model.datDesc = query.datDesc;
                    model.DATID = Convert.ToInt16(query.DATID);
                }
            }
            return View(model);
        }
    }



}